<template>
  <div>
    <el-table
      :data="currentPageData"
      style="width: 100%">
      <el-table-column
        v-for="(item,key) in header"
        :key="key"
        :prop="item.key"
        :label="item.text"
        width="280">
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentpage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  name: 'TableComponent',
  data () {
    return {
      tableData: [],
      header: [],
      pagesize: 10,
      pagesizes: [10, 20, 30, 50],
      currentPageData: [],
      total: 0,
      currentpage: 1
    }
  },
  props: {
      table: {
          type: Object,
          default: null
      }
  },
  watch: {
      // 监视父组件对table的动态修改，因为props的值，不会自动跟父组件绑定
      table(value) {
          this.table = value
          this.tableData = this.table.datas
          this.header = this.table.header
          this.total = this.tableData.length
          this.getCurrentPageData();
      }
  },
  methods: {
      handleSizeChange(val) {
        this.pagesize = val
        this.getCurrentPageData()
      },
      handleCurrentChange(val) {
        this.currentpage = val
        this.getCurrentPageData()
      },
      getCurrentPageData()　{
        this.currentPageData = this.tableData.filter((item,index) => index < this.pagesize * this.currentpage && index >= this.pagesize * (this.currentpage - 1));
      }
  }
}
</script>
